package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Article;
import com.weipai.springboot.model.ArticleCategory;
import com.weipai.springboot.model.Brand;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.ArticleDao;
import com.weipai.springboot.module.service.ArticleService;
import com.weipai.springboot.module.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.RescaleOp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${lyh} on 2018/8/25.  * 文章管理类
 */
@Service
public class ArticleServiceImpl implements ArticleService{

    @Autowired
    ArticleDao articleDao;

    private static final Logger log = LoggerFactory.getLogger(ArtisanServiceImpl.class);

    //新闻列表查询
    @Override
    public List<Article> selectAllByArticle(int articleStatus, String search, String way, String categoryId) {
        return articleDao.selectAllByArticle(articleStatus,search,way, categoryId);
    }

    @Override
    public Response getArticleById(String articleId) {
        Response response = new Response();
        Article article= articleDao.getArticleById(articleId);
        if(null!=article){
            response.setCode(RetCode.SUCCESS);
            response.put("data", article);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;

    }

    //修改
    @Override
    public Response updateArticle(Article article) {
        Response response = new Response();
        if(1==articleDao.updateArticle(article)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //文章的保存
    @Override
    public Response save(Article article) {
        Response response = new Response();
        article.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
        if(1==articleDao.save(article)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //文章的删除
    @Override
    public Response delete(String articleId) {
        Response response = new Response();
        if(1==articleDao.delete(articleId)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //文章分类列表
    @Override
    public Response articleCategoryList() {
        Response response = new Response();
        List<ArticleCategory> articleCategories = articleDao.articleCategoryList();
        if(!articleCategories.isEmpty()){
            response.setCode(RetCode.SUCCESS);
            response.setData(articleCategories);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }
}
